<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/zhuce.css">
    <link rel="stylesheet" href="../css/清除样式.css">
    <link rel="icon" href="https://www.anta.com/favicon.ico">
    <link rel="stylesheet" href="../css/footer.css">
    <link rel="stylesheet" href="../css/header.css">
    <link rel="stylesheet" href="../font/iconfont.css">

</head>

<body>
    <!-- 头部 -->
    <header>

    </header>



    <div id="zhuce">
        <div id="zhuce-1">
            <div id="zhuce-2">
                <div id="zhuce-2-1">
                    <div id="zhuce-2-2">
                        <div id="zhuce-2-3">
                            <h6 class="h">手机注册</h6>
                            <h6>邮箱注册</h6>
                        </div>
                        <div id="zhuce-2-4">
                            <h5>请注册:</h5>
                            <div id="zhuce-2-5">
                                <input id="input1" type="text" placeholder="用户名">
                                <span id="span"></span>
                            </div>
                            <div id="zhuce-2-5" style="border: none;">
                                <div id="zhuce-2-5-1">
                                    <input id="input2" type="text" placeholder="密码" class="iptn">
                                    <span id="span1"></span>
                                </div>
                            </div>
                            <div id="ipt">
                                <input type="checkbox">
                                我已充分阅读、理解并接受
                                <a href="javascript:">个人信息保护政策</a>
                                的全部内容
                            </div>
                            <div id="zhuce-2-6">
                                <input id="btn" type="submit" value="确认">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 底部 -->
    <footer>

    </footer>
</body>

</html>
<script src="../js/jQuery.js"></script>
<script src="../js/index.js"></script>
<script src="../js/ajaxtool.js"></script>
<script>
    let input1 = document.querySelector("#input1");
    let input2 = document.querySelector("#input2");
    let btn = document.querySelector("#btn");

    let flag1 = false;
    let flag2 = false;
    // 正则用户名验证
    let reg = /[^u4e00-u9fa5]/;
    input1.onblur = function () {
        if (reg.test(input1.value)) {
            flag1 = true
            document.getElementById("span").innerHTML = "√";
        } else {
            flag1 = false
            document.getElementById("span").innerHTML = "×";
        }
    }
    //正则密码验证
    let reg1 = /^[0-9]{6,18}$/;
    input2.onblur = function () {
        if (reg1.test(input2.value)) {
            flag2 = true
            document.getElementById("span1").innerHTML = "√";
        } else {
            flag1 = false;
            document.getElementById("span1").innerHTML = "×";
        }
    }

    btn.onclick = function () {
        //发请求
        if (flag1 == true && flag2 == true) {
            //1.创建对象
            let xhr = new XMLHttpRequest();
            // 2.设置请求参数
            xhr.open("POST", "../goodsAndShoppingCart/addUser.php");
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            // 3.发送请求
            // xhr.send('username=zhangsan&password=1234')
            xhr.send(`username=${input1.value}&userpass=${input2.value}`);
            // 4.得到响应数据
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    console.log(xhr.responseText);
                    if (xhr.responseText.trim() == "success") {
                        alert('注册成功');
                        // setTimeout(function () {
                        location.href = '../html/denglu.html';
                        // }, 600)
                    }
                    if (xhr.responseText.trim() == 'fail') {
                        alert('注册失败');
                    }
                }
            }

        }else{
            alert('注册失败');
        }
    }




</script>